/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scms;

/**
 * main program, displays menu and holds inventory instance
 * @author pumped
 */
public class Scms {

	private Inventory inventory = new Inventory();

	/**
	 * runs main scms program
	 */
	public void run() {
		System.out.println("Importing Inventory...");
		inventory.importItems();
		System.out.println("Importing Products...");
		inventory.importProducts();
		switch (StreamIO.readInt("[0]Gui or [1]Console?")) {
			case 0:
				runGui();
				break;
			case 1:
				runConsole();
				break;

			default:

		}
	}

	private void runGui() {
		//create menu window
		ScmsGuiMenu frame = new ScmsGuiMenu();
		frame.showWindow(true);
		frame.setInventory(inventory);
	}

	private void runConsole() {
		int selection;

		System.out.println("Welcome to the scms");

		do {
			showMenu();
			selection = StreamIO.readInt("Choose an Option: ");

			switch (selection) {
				case 1:
					if (selectCategory() == 1) {
						inventory.printProducts();
					} else {
						inventory.printInventory();
					}
					break;
				case 2:
					inventory.edit();
					break;
				case 3:
					if (selectCategory() == 1) {
						int invItem = StreamIO.readInt("Is the product in the Inventory yet? (1)yes / (2)no: ", 1, 2);
						int id;
						if (invItem == 2) {
							id = inventory.addNewItem();
						} else {
							inventory.printInventory();
							id = StreamIO.readInt("What is the inventory items id: ");
						}
						InventoryItem newProduct = inventory.getItemByID(id);
						inventory.addNewProduct(newProduct);
					} else {
						inventory.addNewItem();
					}
					break;
				case 4:
					inventory.placeOrder();
					break;
				case 5:
					System.out.println("Saving...");
					inventory.export();
					break;
				case 6:
					break;
				default:
					System.out.println("Invalid Selection");

			}
		} while (selection != 6);
	}

	/**
	 * asks user preference for category
	 * 1 for Products
	 * 2 for inventory
	 * @return int category user selected
	 */
	private int selectCategory() {
		int input;
		input = StreamIO.readInt("(1)Products or (2)Inventory: ", 1, 2);
		return input;
	}

	/**
	 * shows the programs main menu
	 */
	private void showMenu() {

		//print out menu
		System.out.println("--Menu--");
		System.out.println("1 - Show Products / Inventory");
		System.out.println("2 - Edit Products / Inventory");
		System.out.println("3 - Add Products / Inventory");
		System.out.println("4 - Place order");
		System.out.println("5 - Save to file");
		System.out.println("6 - Exit");

	}
}
